﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Pyramid.Extentions;
using Pyramid.Models;

namespace Pyramid.Mvc.Html
{
	public static class InputExtensions
	{
		/// <summary>
		/// 基于DWZ的日期输入框
		/// </summary>
		public static MvcHtmlString DateInputFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string additionalCssNames = "")
		{
			var css = string.Format("date {0}", additionalCssNames);
			return htmlHelper.TextBoxFor(expression, "{0:yyyy-MM-dd}", new {@class = css, @readonly = "readonly"});
		}

		/// <summary>
		/// 基于DWZ的日期输入框
		/// </summary>
		public static MvcHtmlString DateField(this HtmlHelper htmlHelper, string name, string value, string additionalCssNames = "")
		{
			var css = string.Format("date {0}", additionalCssNames);
			return htmlHelper.TextBox(name, value, new {@class = css, @readonly = "readonly"});
		}

		/// <summary>
		/// 只读文本框
		/// </summary>
		public static MvcHtmlString ReadonlyFieldFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
		{
			return htmlHelper.TextBoxFor(expression, new {@readonly = "readonly"});
		}

		/// <summary>
		/// 只读日期框（短格式）
		/// </summary>
		public static MvcHtmlString ReadonlyDateFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
		{
			return htmlHelper.TextBoxFor(expression, "{0:yyyy-MM-dd}", new {@readonly = "readonly"});
		}

		/// <summary>
		/// 只读日期框（长格式）
		/// </summary>
		public static MvcHtmlString ReadonlyDateTimeFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
		{
			return htmlHelper.TextBoxFor(expression, "{0:yyyy-MM-dd HH:mm:ss}", new {@readonly = "readonly"});
		}

		/// <summary>
		/// 编码输入框
		/// </summary>
		public static MvcHtmlString CodeFieldFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
		{
			return htmlHelper.TextBoxFor(expression, new {@class = "required alphanumeric", minlength = "2", maxlength = "6"});
		}

		/// <summary>
		/// 由范围生成下拉框，用途如：选择年份
		/// </summary>
		public static MvcHtmlString DropDownListForRange(this HtmlHelper htmlHelper, string name, int from, int to, int selected)
		{
			var selectList = new SelectList(Enumerable
												.Range(from, to - from + 1)
												.Select(x => new SelectListItem
													{
														Value = string.Format("{0}", x),
														Text = string.Format("{0}", x)
													}), "Value", "Text", selected);
			return htmlHelper.DropDownList(name, selectList, new {@class = "combox"});
		}

		/// <summary>
		/// 将枚举类型所有值转换成 checkbox 列表
		/// </summary>
		public static MvcHtmlString EnumCheckBoxList<TEnum>(this HtmlHelper htmlHelper, string name, string displayName,
															TEnum selectedValues, TEnum expectedValues) where TEnum : struct
		{
			var enumType = typeof (TEnum);
			if (enumType.IsEnum == false) throw new ArgumentException("Enumeration type is expected.");

			var enums = Enum.GetValues(enumType);
			var html = new StringBuilder();
			var template = "<table><tr><td width='120'>{0}</td><td>";
			html.AppendFormat(template, displayName);

			template = "<label><input type='checkbox' name='{0}' value='{1}' {2}/>{3}</label>";
			foreach (var e in from Enum e in enums where (Convert.ToInt32(e) & Convert.ToInt32(expectedValues)) == 0 select e)
			{
				var checkedHtml = ((Convert.ToInt32(e) & Convert.ToInt32(selectedValues)) == 0) ? "" : "checked='checked'";
				html.AppendFormat(template, name, e.ToString("d"), checkedHtml, e.ToDescription());
			}
			html.Append("</td></tr></table>");

			return MvcHtmlString.Create(html.ToString());
		}

		public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, string displayName,
												List<int> selectedValues, IEnumerable<IdTextModel> source)
		{
			var html = new StringBuilder();
			var template = "<table><tr><td width='120'>{0}</td><td>";
			html.AppendFormat(template, displayName);

			template = "<label><input type='checkbox' name='{0}' value='{1}' {2}/>{3}</label>";
			foreach (var item in source)
			{
				var checkedHtml = (selectedValues.Count > 0 && selectedValues.Contains(item.Id)) ? "checked='checked'" : "";
				html.AppendFormat(template, name, item.Id, checkedHtml, item.Text);
			}
			html.Append("</td></tr></table>");

			return MvcHtmlString.Create(html.ToString());
		}
	}
}